package com.woniuxy.action;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.woniuxy.action.aware.UserAware;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.util.PageUtil;

public class UserAction extends ActionSupport implements ModelDriven<User>,
		RequestAware, SessionAware {

	private User user;
	private Map<String, Object> request;
	private Map<String, Object> session;
	private UserService service;

	/*
	 * 显示用户信息列表
	 */
	public String list() {
		session.put("name", user.getUserName());
		session.put("list", service.showAll());
		session.put("page", PageUtil.getIntance().getPage());
		session.put("total",PageUtil.getIntance().getTotalpage());
		return SUCCESS;
	}

	/*
	 * 注册功能 附Date格式转换 实现类见com.woniuxy.convert.TimeConvert
	 */
	public String register() {

		if (service.addUser(user)) {
			session.put("name", user.getUserName());
			session.put("list", service.showAll());
			return SUCCESS;
		}
		request.put("result", "注册失败");
		return ERROR;

	}

	public String del() {
		if (service.deleteUser(user)) {
			session.put("list", service.showAll());
			request.put("result", "删除成功");
			return "done";
		} else
			request.put("result", "删除失败");
		return "done";
	}

	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public User getModel() {
		// TODO Auto-generated method stub
		this.user = new User();
		this.service = new UserService();
		return user;
	}

}
